<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Mp;
use App\Models\Order;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Carbon;

class MpController extends AdminController
{
    protected $description = [
        'index' => '打包小程序前，请将已经建立的小程序配置信息对应的ID提供给前端',
        //        'show'   => 'Show',
        //        'edit'   => 'Edit',
        //        'create' => 'Create',
    ];

    public function description()
    {
        return $this->description;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Grid::make(new Mp(), function (Grid $grid) {
            $grid->model()->orderByDesc('created_at');
            $grid->column('id')->sortable()->help('提供给前端用来打包小程序的mp值');
            $grid->column('name');
//            $grid->column('ico')->image('', 100, 100);
//            $grid->column('unionid');
//            $grid->column('appid');
//            $grid->column('mch_id');
//            $grid->column('AppSecret');
//            $grid->column('caroot');
            $grid->column('user_count', '总用户')->display(function () {
                return User::where('mps_id', $this->id)->count();

            });
            $grid->column('vip', 'VIP')->display(function () {
                return User::where('mps_id', $this->id)->where('vip_end_time', '>', 0)->count();

            });
            $grid->column('day', '今日新增')->display(function () {
                $start_time = strtotime(Carbon::now()->startOfDay()->toDateTimeString());
                $end_time = strtotime(Carbon::now()->endOfDay()->toDateTimeString());
                return User::whereBetween('created_at', [$start_time, $end_time])->where('mps_id', $this->id)->count();
            });
            $grid->column('chongz', '总充值')->display(function () {
                return Order::where('mps_id', $this->id)->where('status', 1)->sum('price');
            });
            $grid->column('chongz2', '今日充值')->display(function () {
                $start_time = Carbon::now()->startOfDay()->toDateTimeString();
                $end_time = Carbon::now()->endOfDay()->toDateTimeString();
                return Order::whereBetween('created_at', [$start_time, $end_time])->where('mps_id', $this->id)->where('status', 1)->sum('price');
            });
            $grid->column('status')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Mp(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('ico');
            $show->field('unionid');
            $show->field('appid');
            $show->field('AppSecret');
            $show->field('caroot');
            $show->field('callback');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Mp(), function (Form $form) {
            $form->display('id')->help('提供给前端打包的mp');
            $form->text('name')->required();
            $form->image('ico');
            $form->hidden('unionid')->value(rand(1111,9999));
            $form->text('appid')->required();
            $form->text('mch_id')->required();
            $form->text('AppSecret')->required();
            $form->file('care_cert')->disk('admin')->move('/Cert')->autoUpload()->required()->accept('pem')->help('商户平台下载的apiclient_cert.pem');
            $form->file('care_key')->disk('admin')->move('/Cert')->autoUpload()->required()->accept('pem')->help('商户平台下载的apiclient_key.pem');
            $form->url('callback');
            $form->url('notfiy_url')->value('https://qingtingc.youyacao.com/api/')->required();
            $form->text('v3key')->required();
            $form->text('v2key');
            $form->text('token');
            $form->radio('status')->options([0 => '禁用', 1 => '启用'])->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
